/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.pixlib.sfe.view.dialog 
{
	import net.pixlib.core.PixlibApplication;
	import net.pixlib.plugin.Plugin;
	import net.pixlib.sfe.log.Debug;
	import net.pixlib.sfe.util.getTranslation;
	import net.pixlib.sfe.view.ViewList;
	import net.pixlib.sfe.view.abstract.JFrameView;
	import net.pixlib.tms.LocalManager;
	import net.pixlib.tms.LocalManagerEvent;

	import org.aswing.BorderLayout;
	import org.aswing.Insets;
	import org.aswing.JFrame;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JTextArea;
	import org.aswing.border.EmptyBorder;
	import org.aswing.geom.IntDimension;

	import flash.filesystem.File;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;

	/**
	 * 'Error reporting' dialig view.
	 * 
	 * @author Romain Ecarnot
	 */
	public class LogView extends JFrameView
	{
		//--------------------------------------------------------------------
		// Private properties
		//--------------------------------------------------------------------
		
		/** @private */
		private var _input : JTextArea;
		
		
		//--------------------------------------------------------------------
		// Public API
		//--------------------------------------------------------------------
		
		/**
		 * Creates view.
		 */		
		public function LogView(owner : Plugin)
		{
			super(owner, ViewList.LOG_VIEW, new JFrame(PixlibApplication.getInstance().root, "Error report", true));
		}
		
		/**
		 * Overrides View#show() method to deal with JFrame component.
		 */
		public function open(log : String, source : File = null) : void
		{
			var msg : String = "";
			
			if(source != null)
			{
				var lines : Array = log.split("\n");
				var len : uint = lines.length;
				var line : String;
				for(var i : uint = 0; i < len; i++)
				{
					line = lines[i];
					
					if(line.indexOf(source.nativePath) > -1) msg += line + "\n";	
				}
			}
			else msg = log;
			
			Debug.DEBUG(msg, this);
			
			_input.setText(msg);
			
			getFrame().setX(PixlibApplication.getInstance().size.width * 0.5 - getFrame().width * 0.5);			getFrame().setY(PixlibApplication.getInstance().size.height * 0.5 - getFrame().height * 0.5);
			getFrame().show();
						
			super.show();
		}
		
		
		//--------------------------------------------------------------------
		// Protected methods
		//--------------------------------------------------------------------
		
		/**
		 * Inits User Interface.
		 */
		override protected function onInitView( ) : void
		{
			LocalManager.getInstance().addEventListener(LocalManagerEvent.onLocalChangeEVENT, _onLocalChangeHandler);
			
			getFrame().setContentPane(new JPanel(new BorderLayout(5,5)));
			getFrame().filters = [new DropShadowFilter(0, 0, 0x000000, 0.75, 10, 10, 1, BitmapFilterQuality.MEDIUM)];
			
			getPanel().setBorder(new EmptyBorder(null, new Insets(5, 5, 5, 5)));
			
			_input = new JTextArea("");
			_input.setPreferredSize(new IntDimension(350, 200));
			
			getPanel().append(new JScrollPane(_input), BorderLayout.CENTER);
			
			getFrame().pack();
			
			_onLocalChangeHandler();
			
			super.onInitView();		}
		
		
		//--------------------------------------------------------------------
		// Private methods
		//--------------------------------------------------------------------
		
		/** @priavte */
		private function _onLocalChangeHandler(event : LocalManagerEvent = null) : void
		{
			getFrame().setTitle(getTranslation("logDialogTitle"));
		}		
	}
}
